package com.example.japaneseapp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.StringTokenizer;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lc.listword.NewWord;

public class TestNewWordActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test_new_word);

		if (savedInstanceState == null) {
			getFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.test_new_word, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {
		private TextView tvQuestion;
		private int[] btnArr = { R.id.btnanswer1, R.id.btnanswer2,
				R.id.btnanswer3, R.id.btnanswer4 };
		private ArrayList<NewWord> listWord;
		private int rightAnswerPosition;
		private int positionQuestion;
		private Random r;
		private int size;
		private ProgressBar prgTime;
		private Handler mHandler = new Handler();
		private int mProgressStatus = 0;
		private Thread thrQuestionDisplay, thrTimeDisplay;

		public PlaceholderFragment() {

		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_test_new_word,
					container, false);
			tvQuestion = (TextView) rootView.findViewById(R.id.tvquestion);
			prgTime = (ProgressBar) rootView.findViewById(R.id.prgTime);
			prgTime.setMax(1000);

			positionQuestion = 0;
			listWord = new ArrayList<NewWord>();
			readData();
			Collections.shuffle(listWord);
			size = listWord.size();
			thrQuestionDisplay = new Thread(new Runnable() {
				public void run() {
					// Update the progress bar
					mHandler.post(new Runnable() {
						public void run() {
							displayQuestionAnswer(getView(), positionQuestion);
						}
					});
				}
			});

			thrTimeDisplay = new Thread(new Runnable() {
				public void run() {
					thrQuestionDisplay.run();
					while (mProgressStatus <= 1000) {
						mProgressStatus++;
						for (int i = 0; i < 4; i++) {
							Button b;
							b = (Button) getView().findViewById(btnArr[i]);
							b.setOnClickListener(new OnClickListener() {

								@Override
								public void onClick(View v) {
									mProgressStatus = 0;
									positionQuestion++;
									thrQuestionDisplay.run();
								}
							});
						}
						// Update the progress bar
						mHandler.post(new Runnable() {
							public void run() {
								prgTime.setProgress(mProgressStatus);
							}
						});
						try {
							Thread.sleep(5);
						} catch (Exception e) {
							// TODO: handle exception
						}
						if (mProgressStatus == 1000) {
							mProgressStatus = 0;
							positionQuestion++;
							thrQuestionDisplay.run();
						}

					}

				}
			});
			thrTimeDisplay.start();

			return rootView;
		}

		private void displayQuestionAnswer(View rootView, int positionQuestion) {
			r = new Random();
			rightAnswerPosition = r.nextInt(3);
			int i = positionQuestion;
			tvQuestion.setText(listWord.get(i).getJpWord());
			for (int j = 0; j < rightAnswerPosition; j++) {
				int k = i;
				while (k == i) {
					k = r.nextInt(size);
					Button b;
					b = (Button) rootView.findViewById(btnArr[j]);
					b.setText(listWord.get(k).getVnWord());
				}
			}
			for (int j = rightAnswerPosition + 1; j < 4; j++) {
				int k = i;
				while (k == i) {
					k = r.nextInt(size);
					Button b;
					b = (Button) rootView.findViewById(btnArr[j]);
					b.setText(listWord.get(k).getVnWord());
				}
			}
			Button btnRight;
			btnRight = (Button) rootView
					.findViewById(btnArr[rightAnswerPosition]);
			btnRight.setText(listWord.get(i).getVnWord());

		}

		public void readData() {
			InputStream in = getActivity().getResources().openRawResource(
					R.raw.unit1);
			InputStreamReader inReader = new InputStreamReader(in);
			BufferedReader bufferedReader = new BufferedReader(inReader);
			String word, jpWord, vnWord;
			StringTokenizer stringTokenizer;
			while (true) {
				try {
					word = bufferedReader.readLine();
					if (word == null)
						break;
					stringTokenizer = new StringTokenizer(word, "/");
					jpWord = stringTokenizer.nextToken();
					vnWord = stringTokenizer.nextToken();
					NewWord newWord = new NewWord(jpWord, vnWord);
					listWord.add(newWord);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

}
